<?php
/**
 * Created by PhpStorm.
 * User: 17628
 * Date: 2021-09-15
 * Time: 11:43
 */

namespace app\api\controller;

use app\common\controller\Api;
use think\Env;
use app\admin\model\Banner as BannerModel;
use app\admin\model\Commodity as CommodityModel;
use app\admin\model\Commoditycase as CommoditycaseModel;

class Store extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //商城
    public function Store()
    {
        $resultArr = [
            'banner' => $this->banner(), //轮播
            'commodityCate' => $this->commodityCate(), //商城商品分类
            'exchange' => $this->exchange(), //商场商品列表
            'recommend' => $this->recommend(),//商城为你推荐
        ];
        return sucessMsg(200, '请求成功', $resultArr);
    }

    //轮播
    public function banner()
    {
        $model = new BannerModel();
        $list = $model
            ->field('id,image')
            ->select();
        $list = addWebSiteUrl($list, ['image']);
        return $list;
    }

    // 商城商品分类
    public function commodityCate()
    {
        $model = new CommoditycaseModel();
        $list = $model
            ->field('id,name')
            ->select();
        return $list;
    }

    //商城商品列表
    public function exchange()
    {
        $cate_id = input('commoditycase_id');
        $production = CommodityModel::where('commoditycase_id', $cate_id)
            ->field('id,image,title,integral,commoditycase_id')
            ->select();
        $list = addWebSiteUrl($production, ['image']);
        return $list;
    }

    //商城为你推荐
    public function recommend()
    {
        $model = new CommodityModel();
        $list = $model
            ->field('id,title,image,integral')
            ->order('id', 'desc')
            ->limit(0, 4)
            ->select();
        $list = addWebSiteUrl($list, ['image']);
        return $list;
    }

    //商城商品详情页
    public function exchangeDetails()
    {
        $id = input('id');
        if (empty($id)) {
            return errMsg(422, '请输入商品id!');
        }
        $model = new CommodityModel();
        $list = $model
            ->where('id', $id)
            ->field('id,name,content,image,brief,integral,original,num,ensure,place')
            ->find();
        if (empty($list)) {
            return errMsg(422, '找不到数据');
        }
        $list->image = Env::get('database.yuming') . $list->image;
        return sucessMsg(200, '详情获取成功！', ['list' => $list]);
    }
}